package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.DisasterAssEntity;
import com.css.fxfzaqbz.modules.assess.repository.DisasterRepository;
import com.css.fxfzaqbz.modules.assess2.entity.TaskContent;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-09-09 16:15
 */
@Repository
public class DisasterRepositoryImpl implements DisasterRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public DisasterAssEntity saveDisasterAss(DisasterAssEntity disasterAssEntity) {
        String sql = "insert into sys_disaster_assessment  (" +
                "sda_id,sda_ass_sei_intensity,sda_ass_date," +
                "sda_ass_people,sda_ass_file_id,sda_ass_file_titel," +
                "sda_ass_file_describe,create_time," +
                "del_flag,create_user,sda_relation,types," +
                "mark,data_message)" +
                "values(" +
                ":sdaId,:sdaAssSeiIntensity,:sdaAssDate,:sdaAssPeople," +
                ":sdaAssFileId,:sdaAssFileTitel," +
                ":sdaAssFileDescribe,:createTime," +
                ":delFlag,:createUser,:sdaRelation,:types," +
                ":mark,:dataMessage)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(disasterAssEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return disasterAssEntity;
    }

    @Override
    public List<DisasterAssEntity> getAllDisasterAss(String id) {
        String sql="";
        sql="select u.* from sys_disaster_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sda_relation='"+id+"' and u.mark='"+YNEnum.N+"' order by u.sda_ass_date desc ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DisasterAssEntity.class));

    }

    @Override
    public List<DisasterAssEntity> getDisasterAssBySi(String intensiity,String sdaRelation) {
        String sql="";
        sql="select u.* from sys_disaster_assessment u  where  u.sda_ass_sei_intensity='"+intensiity+"' and u.sda_relation='"+sdaRelation+"' and u.del_flag = '"+ YNEnum.N.toString() +"' and u.mark='"+YNEnum.Y+"'";

        try {
            List<DisasterAssEntity> disasterAssEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisasterAssEntity.class));

            return disasterAssEntities;
        } catch (DataAccessException e) {
            return null;
        }

    }

    @Override
    public DisasterAssEntity updateDisasterAss(DisasterAssEntity disasterAssEntity, String sdaId) {
       String sql="";
       sql="update sys_disaster_assessment set sda_ass_sei_intensity=:sdaAssSeiIntensity ,sda_ass_date=:sdaAssDate,sda_ass_people=:sdaAssPeople,sda_ass_file_id=:sdaAssFileId,sda_ass_file_titel=:sdaAssFileTitel,sda_ass_file_describe=:sdaAssFileDescribe where sda_id='"+sdaId+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(disasterAssEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return disasterAssEntity;
    }

    @Override
    public DisasterAssEntity getDisasterAssById(String id) {
        String sql="";
        sql="select u.* from sys_disaster_assessment u  where  u.sda_id='"+id+"' and u.del_flag = '"+ YNEnum.N.toString() +"'";
        DisasterAssEntity disasterAssEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(DisasterAssEntity.class));
        return disasterAssEntity;
    }

    @Override
    public void deleteDisasterAss(String id, String userID, Timestamp currentTimestamp) {
        String sql="";
        sql="update sys_disaster_assessment  set del_flag ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"'where sda_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public DisasterAssEntity updateDisasterAssD(DisasterAssEntity disasterAssEntity) {
        String sql="";
        sql="update sys_disaster_assessment set sda_ass_sei_intensity=:sdaAssSeiIntensity ,sda_ass_people=:sdaAssPeople,sda_ass_file_id=:sdaAssFileId,sda_ass_file_titel=:sdaAssFileTitel,sda_ass_file_describe=:sdaAssFileDescribe,update_user =:updateUser,update_time=:updateTime where sda_id='"+disasterAssEntity.getSdaId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(disasterAssEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return disasterAssEntity;
    }

    @Override
    public List<TaskContent> getAllDisasterAss1(String id) {
        String sql="";
        sql="select sda_id id,sda_ass_sei_intensity as content,sda_ass_date as dateTime from sys_disaster_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sda_relation='"+id+"' ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskContent.class));

    }

    @Override
    public void firstSubmitDisasterAss(String id, String userID, Timestamp currentTimestamp,String describe) {
        String sql="";
        sql="update sys_disaster_assessment  set mark ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"',sda_ass_file_describe='"+describe+"' where sda_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public DisasterAssEntity ifRepetition(DisasterAssEntity disasterAssEntity) {
        String sql="";
        sql="select u.* from sys_disaster_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sda_ass_file_titel='"+disasterAssEntity.getSdaAssFileTitel()+"' and u.sda_relation='"+disasterAssEntity.getSdaRelation()+"' and u.sda_ass_sei_intensity ='"+disasterAssEntity.getSdaAssSeiIntensity()+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(DisasterAssEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<DisasterAssEntity> getBigDisasterTwo(String id) {
        String sql="";
        sql="select u.* from sys_disaster_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sda_relation='"+id+"' and u.mark != '"+YNEnum.N+"' order by u.sda_ass_sei_intensity";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DisasterAssEntity.class));

    }

    @Override
    public void addDescribe(List<String> ids, String describe) {

        String sql = "update sys_disaster_assessment set sda_ass_file_describe = '" + describe + "' " +
                " where sda_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public void removeDisasterAss(DisasterAssEntity disasterAssEntity) {
        String sql="";
        sql="update sys_disaster_assessment  set mark ='"+YNEnum.N.toString()+"',sda_ass_file_describe='"+disasterAssEntity.getSdaAssFileDescribe()+"' ,submiter='"+disasterAssEntity.getSubmiter()+"' ,submit_phone='"+disasterAssEntity.getSubmitPhone()+"' where sda_id = '"+disasterAssEntity.getSdaId()+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void firstSubmitDisasterAss2(String id, String userID, Timestamp currentTimestamp) {
        String sql="";
        sql="update sys_disaster_assessment  set mark ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"' where sda_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void secendSubmitDisasterAss(List<String> ids, SUser sUser,Timestamp currentTimestamp) {
        String sql = "update sys_disaster_assessment set mark = '" + 2 + "', submiter='"+sUser.getUserName()+"',submit_phone='"+sUser.getMobilePhone()+"',update_time='"+currentTimestamp+"',submit_time='"+currentTimestamp+"'" +
                " where sda_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<DisasterAssEntity> getDisasterAssByIaId(String id) {
        String sql = "select * from sys_disaster_assessment where sda_relation='" + id + "' and del_flag='" + YNEnum.N + "' and mark='" + 2 + "'";
        List<DisasterAssEntity> disasterAssEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisasterAssEntity.class));
        return disasterAssEntities;
    }

    @Override
    public List<DisasterAssEntity> getDisasterAssByIaaId(String iaId) {
        String sql = "select * from sys_disaster_assessment where sda_relation='" + iaId + "' and del_flag='" + YNEnum.N + "' and mark='" + YNEnum.Y + "'";
        List<DisasterAssEntity> disasterAssEntities = null;
        try {
            disasterAssEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisasterAssEntity.class));
            return disasterAssEntities;
       } catch (DataAccessException e) {
           return null;
        }

    }


}
